#coding:utf-8
from django.shortcuts import render,render_to_response
from django.http import HttpResponseRedirect,HttpResponse
from django.template import RequestContext
from django.core.paginator import Paginator
from django.contrib.auth.decorators import login_required
from blogs.models import Blog,Files
# Create your views here.
# 
def required_log(view):
    def panduan(request,*args,**kwargs):
        if not request.user.is_authenticated():
            return HttpResponseRedirect('/urlconf/failed/')
        return view(request,*args,**kwargs)
    return panduan

def list(request,page,templateName,objType):
    try:
        objs = objType.objects.all()
        paginator_blog = Paginator(objs,3)
        num_pages = paginator_blog.num_pages
        next_page = 0
        prevous_page = 0
        if page != '':
            if int(page) < 1:
                page = 1
            if int(page) > num_pages:
                page = num_pages
            page_blog = paginator_blog.page(int(page))
            if page_blog.has_next():
                next_page = page_blog.next_page_number()
            if page_blog.has_previous():
                prevous_page = page_blog.previous_page_number()
            return render_to_response(templateName,{'objs':page_blog.object_list,'page_range':paginator_blog.page_range,'next_page':next_page,'prevous_page':prevous_page,'current_page':int(page)})
        else:
            page_blog = paginator_blog.page(1)
            return render_to_response(templateName,{'objs':page_blog.object_list,'page_range':paginator_blog.page_range,'next_page':next_page,'prevous_page':prevous_page,'current_page':page})
    except Exception,e:
        return HttpResponseRedirect('/urlconf/index/')
def detail(request,id):
    id = int(id)
    article = Blog.objects.get(id=id)
    article_prevous_id = 0
    article_next_id = 0
    article_prevous = Blog.objects.raw("select * from blog where id<%s order by id desc limit 0,1",[id])
    article_next = Blog.objects.raw("select * from blog where id>%s order by id asc limit 0,1",[id])
    flag = False
    flag_next = False
    for article_p in article_prevous:
        flag = True
    for article_n in article_next:
        flag_next = True
    if flag:
        article_prevous_id = article_prevous[0].id
    if flag_next:
        article_next_id = article_next[0].id
    article_top_5 = Blog.objects.order_by('-visited_count')[0:6]
    if article.visited_count != None:
        article.visited_count += 1
    else:
        article.visited_count = 1
    article.save()
    return render_to_response('article_detail.html',context_instance=RequestContext(request,{'article':article,'article_top_5':article_top_5,
        'article_prevous_id':article_prevous_id,'article_next_id':article_next_id}))

def response_get(request):
    userForm = UserForm()
    return render_to_response('index.html',{'form':userForm},context_instance=RequestContext(request))

def response_post(request):
    userForm = UserForm(request.POST)
    if userForm.is_valid():
        email = userForm.cleaned_data['email']
        password = userForm.cleaned_data['password']
        return HttpResponseRedirect('/urlconf/success/')
    return render_to_response('index.html',{'form':userForm},context_instance=RequestContext(request))
def success(request):
    return render_to_response('success.html')

def haha(request,templateName):
    return render_to_response(templateName)

@required_log
def auther1(request):
    #if not request.user.is_authenticated():
    #    return HttpResponseRedirect('/urlconf/failed/')
    return render_to_response('auther1.html')

def auther2(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/urlconf/failed')
    return render_to_response('auther2.html')

def auther3(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/urlconf/failed')
    return render_to_response('auther3.html')

def failed(request):
    return render_to_response('failed.html')

def keyword_test(request):
    return render_to_response('keyword_test.html')

def darkchain(request):
    return render_to_response('darkchain.html')

def qiniu(request):
    if request.method == 'POST':
        filename = request.POST.get('filename')
        filesize = request.POST.get('filesize')
    else:
        filename = request.GET.get('filename')
        filesize = request.GET.get('filesize')

    files = Files(filename=filename,filesize=filesize)
    files.save()
    httpres = HttpResponse()
    httpres.write('{"status":200}')
    return httpres
